<template>
  <div>
    <div class="cms-list-header clearfix flex" style="padding:0 24px 16px 0;flex-wrap:wrap">
      <div style="margin-top:16px;flex:auto" class="content-flex-right">
        <el-select @change="goQuery()" v-model="params.queryChannelId" clearable placeholder="请选择">
          <el-option v-for="item in channelList" :key="item.channelId" :label="item.channelName"
                     :value="item.channelId">
          </el-option>
        </el-select>
        <cms-input label="标题" v-model="params.title"></cms-input>
        <el-button @click="query">查询</el-button>
      </div>
    </div>
    <span style="font-size: 20px;color: #FFA500">已关联文章</span>
    <el-table :data="tableDataLike" stripe v-loading="loading">
      <el-table-column prop="id" label="id" width="45" align="left"></el-table-column>
      <el-table-column prop="title" label="标题" align="left" min-width="200px">
        <div slot-scope="scope">
          <span class="cms-channel-name" style="color: #999;">[{{scope.row.channelName}}]</span>
          {{scope.row.title}}
        </div>
      </el-table-column>
      <el-table-column label="编辑" prop="userName" align="center" width="70"></el-table-column>
      <el-table-column prop="likeSort" label="关联排序" align="center" width="165">
        <div slot-scope="scope">
          <el-input-number style="width: 120px; line-height: 30px;" v-model="scope.row.likeSort" @change="handleChangeLikeSort(scope.row)"  max="5" label="关联排序"></el-input-number>
        </div>
      </el-table-column>
      <el-table-column label="发布时间" prop="releaseDate" align="center" width="120"></el-table-column>
      <el-table-column label="操作" align="center" width="90">
        <div slot-scope="scope">
          <el-button type="primary" @click="deleteContentLike(scope.row.contentLikeId)">取消关联</el-button>
        </div>
      </el-table-column>
    </el-table>
    <span style="font-size: 20px;color: #FFA500;">未关联文章</span>
    <el-table :data="tableData" stripe @selection-change="checkIds" v-loading="loading">
      <el-table-column prop="id" label="id" width="45" align="left"></el-table-column>
      <el-table-column prop="title" label="标题" align="left" min-width="200px">
        <div slot-scope="scope">
          <span class="cms-channel-name" style="color: #999;">[{{scope.row.channelName}}]</span>
          {{scope.row.title}}
        </div>
      </el-table-column>
      <el-table-column label="编辑" prop="userName" align="center" width="70"></el-table-column>
      <el-table-column label="发布时间" prop="releaseDate" align="center" width="120"></el-table-column>
      <el-table-column label="操作" align="center" width="90">
        <div slot-scope="scope">
          <el-button type="primary" @click="associatedContent(scope.row.id)">关联</el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="cms-list-footer" style="padding-bottom:100px;">
      <div class="cms-left"></div>
      <cms-pagination :total="total" @change="getPages"></cms-pagination>
    </div>
  </div>
</template>

<script>
  import axios from "axios";
  import va from "@/rules";
  import formMixns from "@/mixins/form";
  import listMixins from '@/mixins/list'

  export default {
    name: 'content-like',
    mixins: [formMixns, listMixins],
    props: {
      contentId: "",
      typeId: "",
      channelId: ""
    },
    data() {
      return {
        params: {
          status: '2',
          likeIds: "",
          typeId: '',
          channelId: '',
          title: "",
          queryChannelId:""
        },
        tableDataLike: [],
        isShowDialog: false,
      }
    },
    methods: {
      associatedContent(likeId) {
        this.$confirm('确定关联该文章？', '警告', {type: "error"}).then(mes => {
          this.$http.post(this.$api.setContentLike, {contentId: this.contentId, likeId: likeId}).then(res => {
            console.log(res);
            if (res.code == "200") {
              this.successMessage('关联成功');
              this.getContentLikeData();
            }
          });
        }).catch(error => {
          console.log(error);
        });
      },
      getContentLikeData() {
        this.$http.post(this.$api.getContentLikeData, {contentId: this.contentId}).then(res => {
          if (res.code == "200") {
            let contentLikeList = res.body;
            this.tableDataLike = contentLikeList;
            let likeIds = [];
            for (let i in contentLikeList) {
              let contentLike = contentLikeList[i];
              likeIds.push(contentLike.likeId)
            }
            this.params.likeIds = likeIds.join(",");
            this.params.typeId = this.typeId;
            this.params.channelId = this.channelId;
            this.params.contentId = this.contentId;
            this.initTableData(this.$api.likeViewNewsPageList, this.params) //请求列表
          }
        });
      },
      deleteContentLike(contentLikeId) {
        this.$http.post(this.$api.deleteContentLike, {contentLikeId: contentLikeId}).then(res => {
          console.log(res);
          if (res.code == "200") {
            this.getContentLikeData();
          }
        });
      },
      handleChangeLikeSort(contentLike){
        this.$http.post(this.$api.updateContentLike, contentLike).then(res => {
          console.log(res);
          if (res.code == "200") {
            this.successMessage('关联成功');
            this.getContentLikeData();
          }
        });
      },
      getChannelList() {
        axios.post(this.$api.getOneLevelChannelList, {}).then(res => {
          if (res.code == '200') {
            this.channelList = res.body;
          }
        })
      }
    },
    created() {
      this.isShowDialog = true
    },
    mounted() {
      this.getChannelList();
      this.getContentLikeData();
    }
  }
</script>
